<?php

/* --------------------
	Łukasz Krawczyk
    Maciej Gowin
    Cracow 2009
-------------------- */

class DateTimeParser
{
	private $date;
	private $parsed_date;
	
	public function __construct($date = null)
	{
		$this->date = $date === null ? date('Y-m-d H:i:s') : $date;
		$this->parse();
	}
	
	public function parse()
	{
		if($this->date === null)
			return;
		$this->parsed_date = date_parse($this->date);
	}
	
	public static function getCurrentDateTime($format = null)
	{
		if(!$format)
			$format = 'Y-m-d H:i:s';
		return date($format);
	}
	
	public function get($type)
	{
		return $this->parsed_date === null ? null : $this->parsed_date[$type];
	}
	
	public function getDay()
	{
		return $this->get('day');
	}
	
	public function getMonth()
	{
		return $this->get('month');
	}
	
	public function getYear()
	{
		return $this->get('year');
	}
	
	public function getHour()
	{
		return $this->get('hour');
	}
	
	public function getMinute()
	{
		return $this->get('minute');
	}
	
	public function getSecond()
	{
		return $this->get('second');
	}
	
	public function getTime()
	{
		$hour_text = $this->getHour() < 10 ? '0'.$this->getHour() : $this->getHour();
		$minute_text = $this->getMinute() < 10 ? '0'.$this->getMinute() : $this->getMinute();
		$second_text = $this->getSecond() < 10 ? '0'.$this->getSecond() : $this->getSecond();
		return $hour_text.':'.$minute_text.':'.$second_text;
	}
	
	public function getDate()
	{
		$year_text = $this->getYear() < 10 ? '0'.$this->getYear() : $this->getYear();
		$month_text = $this->getMonth() < 10 ? '0'.$this->getMonth() : $this->getMonth();
		$day_text = $this->getDay() < 10 ? '0'.$this->getDay() : $this->getDay();
		return $year_text.'-'.$month_text.'-'.$day_text;
	}
	
	public function getDateTime()
	{
		return $this->getDate().' '.$this->getTime();
	}
	
	public static function constructTime($hour = 0, $minute = 0, $second = 0)
	{
		$hour_text = $hour < 10 ? '0'.$hour : $hour;
		$minute_text .= $minute < 10 ? '0'.$minute : $minute;
		$second_text .= $second < 10 ? '0'.$second : $second;
		return $hour_text.':'.$minute_text.':'.$second_text;
	}
}

?>